import { Prisma } from '@prisma/client';
import { Type } from 'class-transformer';
import {
  IsDefined,
  Matches,
  ValidateIf,
  ValidateNested,
} from 'class-validator';
import { BaseQueryDto, WhiteListItem } from './base.dto';

export class UserQueryDto extends BaseQueryDto<Prisma.UserWhereUniqueInput> {}

export class UserInviteeQueryDto extends BaseQueryDto<Prisma.UserWhereUniqueInput> {
  type: number;
  userId?: number;
}

export class UserLoginDto {
  type?: 'captcha' | 'password';

  @IsDefined({ message: '手机号必填' })
  mobile: string;

  @ValidateIf((o) => o.type === 'captcha')
  @IsDefined({ message: '验证码必填' })
  captcha?: string;

  @ValidateIf((o) => o.type === 'password')
  @IsDefined({ message: '密码必填' })
  pass?: string;

  code?: string;
}

export class UserUpdateDto {
  nickname?: string;
  avatar?: string;
  isHideHold?: boolean;
  isHideOrder?: boolean;
}

export class UserPassDto {
  @IsDefined({ message: '验证码必填' })
  captcha: string;

  @IsDefined({ message: '密码必填' })
  pass: string;

  @IsDefined({ message: '密码确认必填' })
  passConfirm: string;
}

export class UserPass0Dto {
  @IsDefined({ message: '验证码必填' })
  captcha: string;

  @IsDefined({ message: '密码必填' })
  // @MinLength(8, { message: '密码至少8位' })
  @Matches(
    /^(?=.*[a-z])(?=.*\d)[A-Za-z\d$@$!%*?&]{8,}/,
    {
      message:
        '密码必须包含至少一个字母，一个数字，且长度至少为8个字符',
    },
  )
  pass: string;

  @IsDefined({ message: '密码确认必填' })
  passConfirm: string;
}

export class UserRegisterItem {
  @IsDefined({ message: '手机号必填' })
  mobile: string;

  realname?: string;

  idCard?: string;
}

export class UserRpaImportDto {
  @ValidateNested({ each: true })
  @Type(() => UserRegisterItem)
  rows?: UserRegisterItem[];
}

export class UserAllowBatchDto {
  isBatch: boolean;
  @Type(() => UserRpaImportDto)
  rows?: WhiteListItem[];
}
